package com.jcoinexchange.web.auth;

import com.jcoinexchange.web.exception.InternalException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

/**
 * 登录验证 AOP
 */
@Aspect
public class LoginAuthorizeAspect {

    /**
     * 登录验证
     *
     * @param joinPoint
     */
    @Around("@annotation(com.jcoinexchange.web.auth.AuthNeeded)")
    public Object doLoginAuthVerify(ProceedingJoinPoint joinPoint) throws Throwable {
        try{
            assert null != Auth.getCurrentAuthToken();
            assert null != Auth.getCurrentUser();
            return joinPoint.proceed();
        }catch (AssertionError e){
            throw new InternalException(ErrorCode.ERROR_NOT_AUTHED);
        }
    }

    @Pointcut("@annotation(com.jcoinexchange.web.annotation.LoginInNeeded)")
    public void loginVerify() {
    }
}
